package grape.composite.rest.activity.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.base.rest.client.dict.DictClient;
import grape.base.service.dict.po.Dict;
import grape.common.exception.runtime.RBaseException;
import grape.composite.rest.activity.form.*;
import grape.composite.service.activity.api.IActivityContentService;
import grape.composite.service.activity.po.ActivityContent;
import grape.composite.service.activity.query.ActivityListQuery;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;

import grape.composite.rest.activity.vo.ActivityVo;
import grape.composite.rest.activity.mapper.ActivityWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.composite.service.activity.po.Activity;
import grape.composite.service.activity.api.IActivityService;
import java.util.List;
/**
 * <p>
 * 活动 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-08-25
 */
@RestController
@RequestMapping("/composite//activity")
@Api(tags = "活动相关接口")
public class ActivityController extends BaseController<ActivityVo, Activity> {

    @Autowired
    private ActivityWebMapper currentWebMapper;
    @Autowired
    private IActivityService currentService;
    @Autowired
    private IActivityContentService iActivityContentService;

    @Autowired
    private DictClient dictClient;

     @ApiOperation("添加活动")
     @PreAuthorize("hasAuthority('activity:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public ActivityVo create(@RequestBody @Valid ActivityCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }

         Activity po = currentWebMapper.formToPo(cf);
         po.setIsPublished(false);
         return super.create(po);
     }

     @ApiOperation("根据ID查询活动")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public ActivityVo queryById(@PathVariable String id,@ApiParam("是否包含内容") Boolean includeContent) {

         ActivityVo activityVo = super.queryById(id);
         if (activityVo != null && includeContent != null && includeContent) {
             ActivityContent byActivityId = iActivityContentService.getByActivityId(id);
             if (byActivityId != null) {
                 activityVo.setContent(byActivityId.getContent());
             }
         }
         return activityVo;
     }

     @ApiOperation("删除活动")
     @PreAuthorize("hasAuthority('activity:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新活动")
     @PreAuthorize("hasAuthority('activity:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public ActivityVo update(@PathVariable String id,@RequestBody @Valid ActivityUpdateForm uf) {
         Activity po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }
    @ApiOperation("更新活动发布状态")
    @PreAuthorize("hasAuthority('activity:single:updateIsPublished')")
    @PatchMapping("/{id}/publish")
    @ResponseStatus(HttpStatus.CREATED)
    public ActivityVo updateIsPublished(@PathVariable String id,@RequestBody @Valid ActivityUpdateIsPublishedForm uf) {
        Activity po = currentWebMapper.formToPo(uf);
        po.setId(id);
        return super.update(po);
    }

    @ApiOperation("不分页查询活动")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<ActivityVo> list(ActivityListForm listForm) {
        Activity po = currentWebMapper.formToPo(listForm);

        setCategoryDictIdByCode(po, listForm.getCategoryDictCode());

        ActivityListQuery activityListQuery = new ActivityListQuery();
        activityListQuery.setPoQuery(po);
        return super.list(activityListQuery);
    }

    @ApiOperation("分页查询活动")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<ActivityVo> listPage(ActivityListPageForm listPageForm) {
         Activity po = currentWebMapper.formToPo(listPageForm);

        setCategoryDictIdByCode(po, listPageForm.getCategoryDictCode());

        ActivityListQuery activityListQuery = new ActivityListQuery();
        activityListQuery.setPoQuery(po);
         return super.listPage(activityListQuery,listPageForm);
     }

    private void setCategoryDictIdByCode(Activity po,String code) {
        if (isStrEmpty(code)) {
            return;
        }
        Dict dictByCode = dictClient.getDictByCode(code);
        if (dictByCode == null) {
            return;
        }
        po.setCategoryDictId( dictByCode.getId());
    }
}
